import axios from 'axios'
// import store from '@/store'
// import { getToken } from '@/utils/auth'
import {alert} from '@/utils/message';
// 创建axios实例
const service = axios.create({
  // 设置根路径
  // withCredentials: true, // send cookies when cross-domain requests
  // 设置超时时间
  timeout: 5000
})

// 请求拦截器
service.interceptors.request.use(
  // 请求成功
  config => {
    // do something before request is sent

    config.headers['authorization'] = '	Bearer ' + localStorage.getItem('TOKEN')
    return config
  },
  // 请求失败
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  // 响应成功
  response => {
    const res = response.data
    return res
  },
  // 响应失败
  error => {
    // console.log('err' + error) // for debug
    alert(error)
    return Promise.reject(error)
  }
)

export default service
